#include "ImageLabelModel.h"
#include "ImageModel.h"

QString ImageLabelModel::_imageLabelSaveFilePath_ = "";
QString ImageLabelModel::_imageName_ = "";

ImageLabelModelData::ImageLabelModelData(const int &seq, const QString &name, const QString &color,
                    const int &x1, const int &y1, const int &x2, const int &y2) {
    this->seq = seq;
    this->name = name;
    this->color = color;
    this->x1 = x1;
    this->y1 = y1;
    this->x2 = x2;
    this->y2 = y2;
}




ImageLabelModelData::~ImageLabelModelData() {
}

QString ImageLabelModelData::getName() const {
    return this->name;
}

QString ImageLabelModelData::getColor() const {
    return this->color;
}

int ImageLabelModelData::getX1() const {
    return this->x1;
}
int ImageLabelModelData::getX2() const {
    return this->x2;
}

int ImageLabelModelData::getY1() const {
    return this->y1;
}

int ImageLabelModelData::getY2() const {
    return this->y2;
}
int ImageLabelModelData::getSeq() const {
    return this->seq;
}

QJsonObject ImageLabelModelData::toJsonObject() {
    QJsonObject json;
    json.insert(QStringLiteral("name"), this->name);
    json.insert(QStringLiteral("color"), this->color);
    json.insert(QStringLiteral("x1"), this->x1);
    json.insert(QStringLiteral("x2"), this->x2);
    json.insert(QStringLiteral("y1"), this->y1);
    json.insert(QStringLiteral("y2"), this->y2);
    return json;
}



ImageLabelModel::ImageLabelModel(QString imageName) {
    this->_imageName_ = imageName;
}


ImageLabelModel::ImageLabelModel(QObject *parent) : QAbstractListModel(parent) {

}

ImageLabelModel::~ImageLabelModel() {
}

QString ImageLabelModel::imageName() const {
    return this->_imageName_;
}

int ImageLabelModel::rowCount(const QModelIndex &parent) const  {
    Q_UNUSED(parent);
    return _dataList_.count();
}

QVariant ImageLabelModel::data(const QModelIndex &index, int role) const {
    int row = index.row();

    if(row < 0 || row >= _dataList_.count()) {
        return QVariant();
    }
    const ImageLabelModelData &modelData = _dataList_[row];
    switch(role) {
        case ROLE_NAME:
            return modelData.getName();
            break;
        case ROLE_COLOR:
            return modelData.getColor();
            break;
        case ROLE_X1:
            return modelData.getX1();
            break;
        case ROLE_X2:
            return modelData.getX2();
            break;
        case ROLE_Y1:
            return modelData.getY1();
            break;
        case ROLE_Y2:
            return modelData.getY2();
            break;
    }
    return QVariant();
}

QHash<int, QByteArray> ImageLabelModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[ROLE_NAME] = "name";
    roles[ROLE_COLOR] = "color";
    roles[ROLE_X1] = "x1";
    roles[ROLE_X2] = "x2";
    roles[ROLE_Y1] = "y1";
    roles[ROLE_Y2] = "y2";
    return roles;
}


BaseResult::JsonResult ImageLabelModel::get(const int &index) {
    return BaseResult(BaseResult::SUCCEED, "获取成功",this->_dataList_[index].toJsonObject()).toJson();

}

BaseResult::JsonResult ImageLabelModel::append(const int &seq, const QString &name, const QString &color,
                              const int &x1, const int &y1, const int &x2, const int &y2) {
    ImageLabelModelData newModelData(seq, name, color, x1, y1, x2, y2);
    _dataList_.append(newModelData);
    return BaseResult(BaseResult::SUCCEED, "添加成功").toJson();
}


BaseResult::JsonResult ImageLabelModel::save() {
    if(this->_imageLabelSaveFilePath_.isNull() || this->_imageLabelSaveFilePath_.isEmpty()) {
        throw "未选择保存路径，请先选择---------";
    }
//    if(_dataList_.count() <= 0) {
//        return BaseResult(BaseResult::SUCCEED, "保存成功").toJson();
//    }
    QFile file(ImageLabelModel::_imageLabelSaveFilePath_+ "/" + QFileInfo(this->_imageName_).baseName() + ".txt");
    if(file.open(QIODevice::WriteOnly)) {
        QTextStream stream(&file);
        stream.setEncoding(QStringConverter::Utf8);
        for(int i = 0; i < _dataList_.count(); i++) {
            stream << _dataList_[i].getSeq() << " "
                   << _dataList_[i].getX1() << " "
                   << _dataList_[i].getY1() << " "
                   << _dataList_[i].getX2() << " "
                   << _dataList_[i].getY2() << "\n";
        }
        return BaseResult(BaseResult::SUCCEED, "保存成功").toJson();
    } else {
        throw "文件打开失败!";
    }
}

void ImageLabelModel::setImageLabelSaveFilePath(const QString &savePath) {
    ImageLabelModel::_imageLabelSaveFilePath_ = savePath;
}

void ImageLabelModel::setImageName(const QString &imageName) {
    ImageLabelModel::_imageName_ = imageName;
}

BaseResult::JsonResult ImageLabelModel::resetModel(const int &index) {
    QT_TRY {
        this->save();
        loadImage(index);
        return BaseResult(BaseResult::SUCCEED, "调整成功").toJson();
    } QT_CATCH(char const* e) {
        return BaseResult(BaseResult::FAILED, e).toJson();
    }
}


void ImageLabelModel::loadImage(const int &index) {
    emit beginResetModel();
    this->_imageName_ = ImageModel::getInstance()->getImageName(index);
    this->_imageLabelSaveFilePath_ = ImageModel::getInstance()->getImageLabelSaveFilePath(index);
    QFile file(_imageLabelSaveFilePath_+ "/" + QFileInfo(this->_imageName_).baseName() + ".txt");
    this->_dataList_.clear();
    if(file.exists()) {
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
              throw "文件打开失败!!!!";
        }
        QTextStream stream(&file);
        stream.setEncoding(QStringConverter::Utf8);
        QString line;
        while(!stream.atEnd()) {
            line = stream.readLine();
            QStringList qStringList = line.split(' ');
            QT_TRY {
                LabelModelData labelModelData = LabelModel::getFromSeq(qStringList[0].toInt());
                this->append(labelModelData.seq(), labelModelData.labelName(), labelModelData.labelColor(),
                             qStringList[1].toInt(), qStringList[2].toInt(), qStringList[3].toInt(), qStringList[4].toInt());
            } QT_CATCH(char const* e) {
                continue;
            }
        }
    }
    emit endResetModel();
}


void ImageLabelModel::clearAll() {
    emit beginResetModel();
    this->_dataList_.clear();
    QFile file(ImageLabelModel::_imageLabelSaveFilePath_+ "/" + QFileInfo(this->_imageName_).baseName() + ".txt");
    file.remove();
    emit endResetModel();
}

BaseResult::JsonResult ImageLabelModel::removeLast() {
    QT_TRY {
        if(!this->_dataList_.empty()) {
            emit beginResetModel();
            this->_dataList_.removeLast();
            //this->save();
            emit endResetModel();
        }
        return BaseResult(BaseResult::SUCCEED, "撤销成功").toJson();
    } QT_CATCH(char const* e) {
        return BaseResult(BaseResult::SUCCEED, e).toJson();
    }




}
